package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.PhotoAlbumAddDTO;
import com.oa.admin.entity.Photo;
import com.oa.admin.service.IPhotoService;
import com.oa.admin.vo.PhotoAlbumListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.PhotoAlbum;
import com.oa.admin.mapper.PhotoAlbumMapper;
import com.oa.admin.service.IPhotoAlbumService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 相册表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class PhotoAlbumServiceImpl extends ServiceImpl<PhotoAlbumMapper, PhotoAlbum> implements IPhotoAlbumService {

    private final PhotoAlbumMapper photoAlbumMapper;

    @Autowired
    private IPhotoService photoService;

    @Autowired
    public PhotoAlbumServiceImpl(PhotoAlbumMapper photoAlbumMapper) {
        this.photoAlbumMapper = photoAlbumMapper;
    }

    @Override
    @CommonService
    public PageResult<PhotoAlbumListVO> findList(Integer pageNum, Integer pageSize) {
        Page page = new Page();
        return new PageResult(photoAlbumMapper.findPhotoAlbumList(page), page);
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public PhotoAlbum findPhotoAlbumById(String id){
        return photoAlbumMapper.findById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Resp add(PhotoAlbumAddDTO photoAlbumAddDTO) {
        PhotoAlbum photoAlbum = new PhotoAlbum();
        BeanUtils.copyProperties(photoAlbumAddDTO, photoAlbum);
        photoAlbum.setCreateTime(new Date());
        if(insert(photoAlbum)){
            if(photoAlbumAddDTO.getPhotoAddDTOList() != null && photoAlbumAddDTO.getPhotoAddDTOList().size() > 0){
                List<Photo> photos = new ArrayList<>();
                photoAlbumAddDTO.getPhotoAddDTOList().forEach(d -> {
                    Photo photo = new Photo();
                    BeanUtils.copyProperties(d, photo);
                    photo.setPhotoAlbum(photoAlbum.getId());
                    photo.setCreateTime(new Date());
                    photos.add(photo);
                });
                if(photoService.insertBatch(photos)){
                    return new Resp().success();
                }
                return new Resp().failure();
            }
            return new Resp().success();
        }
        return new Resp().failure();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Resp del(String id) {
        if(deleteById(id)){
            Wrapper wrapper = new EntityWrapper().eq("photo_album", id);
            if(photoService.selectCount(wrapper) > 0){
                photoService.delete(wrapper);
            }
            return new Resp().success();
        }
        return new Resp().failure();
    }
}
